﻿using RpcClient.Interface;
using RpcHelper;
using RpcModel;

namespace RpcClient.Tran.Tcc
{
    internal class TccTranTemplate<T> : ITranTemplate where T : class
    {
        /// <summary>
        /// 事件器
        /// </summary>
        private readonly IUnityCollect _Unity = null;
        /// <summary>
        /// 事务模板
        /// </summary>
        /// <param name="name">事务名</param>
        /// <param name="action">回滚的方法</param>
        public TccTranTemplate(string name)
        {
            this.TranMode = RpcTranMode.Tcc;
            this.TranName = name;
            this._Unity = RpcClient.Unity;
        }
        /// <summary>
        /// 事务名
        /// </summary>
        public string TranName
        {
            get;
        }
        /// <summary>
        /// 事务执行方式
        /// </summary>
        public RpcTranMode TranMode
        {
            get;
            set;
        }

        public void Commit(string json, string extend)
        {
            T data = json.Json<T>();
            IRpcTranEvent<T> action = _Unity.Resolve<IRpcTranEvent<T>>(this.TranName);
            action.Commit(data, extend);
        }
        /// <summary>
        /// 回滚
        /// </summary>
        /// <param name="json"></param>
        /// <param name="extend"></param>
        public void Rollback(string json, string extend)
        {
            T data = json.Json<T>();
            IRpcTranEvent<T> action = _Unity.Resolve<IRpcTranEvent<T>>(this.TranName);
            action.Rollback(data, extend);
        }
    }
}
